package ltd.followkba.page;

import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;

import com.kcbsdk.AFileBase;
import com.kcbsdk.EventHandler;
import com.kcbsdk.MyManage.AUserFileType;
import com.kcbsdk.MyWorks.AFileInfo;
import com.kcbsdk.MyWorks.AFileType;
import com.kcbsdk.MyWorks.AKclFile;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import ltd.advskin.MRAM;
import ltd.advskin.MSKIN;
import ltd.advskin.base.BasePage;

import ltd.advskin.base.KcTwoListener;
import ltd.advskin.base.KcTypeListener;
import ltd.advskin.task.TaskFree;
import ltd.advskin.utils.SubUtils;
import ltd.advskin.view.KcBtnProgress;
import ltd.advskin.view.KcHeaderView;
import ltd.followkba.MAPP;
import ltd.followkba.MDBS;
import ltd.followkba.R;
import ltd.followkba.task.App32;
import ltd.followkba.task.App33;
import ltd.followkba.task.BAppUser;
import ltd.followkba.task.BAppWorks;
import ltd.followkba.task.BCommentInfo;
import ltd.followkba.task.BDbsAccount;
import ltd.followkba.task.EditUtils;
import main.MAPI;

import static ltd.advskin.MSKIN.MLOGE;
import static ltd.advskin.MSKIN.MSTRING;
import static ltd.advskin.MSKIN.MTOAST;
import static main.MAPI.MSLEEP;
import static main.MAPI.mMyUtils;
import static main.MAPI.mMyWorks;

public class SendToPage extends BasePage {
    private View kbiCoverSendTo;
    private View kbtWorksSendTo;
    private View tvStateSendTo;
    private View etIntroSendTo;
    private RadioButton rbComment;
    private RadioButton rbGiftSendTo;
    private RadioButton rbShareSendTo;
    private RadioButton rbDownSendTo;
    private RadioButton rbListenSendTo;
    private RadioButton rbLikeSendTo;
    private RadioButton rbCmtLike;
    private RadioButton rbAddFans;
    private RadioButton rbMoney;

    private View clWorksSendTo;
    private EditText etStepSendTo;
    private EditText etQtySendTo;
    private EditText etCommentSendTo;
    private View kbtCommentSendTo;
    private View tvSuccessSendTo;
    private View clCommentSendTo;
    private View clGiftSendTo;
    private View clLikeSendTo;
    private View kbtLikeSendTo;
    private KcBtnProgress kbtSendSendTo;

    private BAppUser mBAppUser;
    private BAppWorks mBAppWorks;
    private byte gWorksType;
    private String[] mComment;
    private int gSelect;
    private int gCounter;
    private int gSuccess;
    private String gsMessage;
    private String gsResult;
    private List<BDbsAccount> mBDbsAccount;
//    private App33[] mApp33;
//    private App32[] mApp32;
    private int gAppQty;
    private int gAppCount;
    private String gsLyric;

    // 所有的页面都接受同样的方式打开LOG
    public static boolean logEnable;

    public void MLOG(String text) {
        if (logEnable) {
            MSTRING(MAPI.GET_LOG_NAME(this) + text);
        }
    }

    @Override
    public void onInitView() {                       // 使用者需要继承，初始化页面控件等显示
        setLayout(R.layout.page_send_to);
        boolean allAccount = false;
        if (mBaseInput != null) {                   // 可能仅仅需要刷新，未必是重新开始的
            mBAppWorks = mBaseInput.getInput(0);
            allAccount = mBaseInput.getInput(1);
            mBaseInput = null;                  // 输入参数的使命已经完成，记得及时销毁之
        }

        mKcHeaderView = findViewById(R.id.khvSendTo);
        gWorksType = MAPP.mBDbsAccount.WorksType;
        String text = String.format("手动操作%s",  MAPP.mWorksName[gWorksType]);
        mKcHeaderView.setTitle(text, new KcTypeListener() {
            @Override
            public void onMessage(int type) {
                switch (type) {
                    case KcHeaderView.TYPE_CLICK_LEFT:
                        onFinish();
                        break;
                }
            }
        });
        kbiCoverSendTo = findViewById(R.id.kbiCoverSendTo);
        kbtWorksSendTo = findViewById(R.id.kbtWorksSendTo);
        tvStateSendTo = findViewById(R.id.tvStateSendTo);
        etIntroSendTo = findViewById(R.id.etIntroSendTo);
        rbComment = findViewById(R.id.rbComment);
        rbGiftSendTo = findViewById(R.id.rbGiftSendTo);
        rbShareSendTo = findViewById(R.id.rbShareSendTo);
        rbDownSendTo = findViewById(R.id.rbDownSendTo);
        rbListenSendTo = findViewById(R.id.rbListenSendTo);
        rbLikeSendTo = findViewById(R.id.rbLikeSendTo);
        rbCmtLike = findViewById(R.id.rbCmtLike);
        rbAddFans = findViewById(R.id.rbAddFans);
        rbMoney = findViewById(R.id.rbMoney);
        clWorksSendTo = findViewById(R.id.clWorksSendTo);
        etStepSendTo = findViewById(R.id.etStepSendTo);
        kbtCommentSendTo = findViewById(R.id.kbtCommentSendTo);
        etCommentSendTo = findViewById(R.id.etCommentSendTo);
        etQtySendTo = findViewById(R.id.etQtySendTo);
        tvSuccessSendTo = findViewById(R.id.tvSuccessSendTo);
        clCommentSendTo = findViewById(R.id.clCommentSendTo);
        clGiftSendTo = findViewById(R.id.clGiftSendTo);
        clLikeSendTo = findViewById(R.id.clLikeSendTo);
        kbtLikeSendTo = findViewById(R.id.kbtLikeSendTo);
        kbtSendSendTo = findViewById(R.id.kbtSendSendTo);

        CompoundButton.OnCheckedChangeListener onListener1 = new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                MLOG("buttonView.getId() "+buttonView.getId());
                rbComment.setChecked(false);
                rbGiftSendTo.setChecked(false);
                rbShareSendTo.setChecked(false);
                rbDownSendTo.setChecked(false);
                rbListenSendTo.setChecked(false);
                rbLikeSendTo.setChecked(false);
                rbCmtLike.setChecked(false);
                rbAddFans.setChecked(false);
                rbMoney.setChecked(false);

                int select = 0;
                switch (buttonView.getId()) {
                    case R.id.rbComment: rbComment.setChecked(true); select = 0; break;
                    case R.id.rbGiftSendTo: rbGiftSendTo.setChecked(true); select = 1; break;
                    case R.id.rbShareSendTo: rbShareSendTo.setChecked(true); select = 2; break;
                    case R.id.rbDownSendTo: rbDownSendTo.setChecked(true); select = 3; break;
                    case R.id.rbListenSendTo: rbListenSendTo.setChecked(true); select = 4; break;
                    case R.id.rbLikeSendTo: rbLikeSendTo.setChecked(true); select = 5; break;
                    case R.id.rbCmtLike: rbCmtLike.setChecked(true); select = 6; break;
                    case R.id.rbAddFans: rbAddFans.setChecked(true); select = 7; break;
                    case R.id.rbMoney: rbMoney.setChecked(true); select = 8; break;
                }
                MDBS.setFreeInt(gWorksType, "OperationSelect", select);
                MLOG(String.format("setCheckedA C %s", select));
                MDBS.setFreeInt(gWorksType, "SendToionSelect", select);
                clWorksSendTo.setVisibility((select >= 7) ? View.GONE : View.VISIBLE);
                setSelect(select);
            }
        };

        rbComment.setOnCheckedChangeListener(onListener1);
        rbGiftSendTo.setOnCheckedChangeListener(onListener1);
        rbShareSendTo.setOnCheckedChangeListener(onListener1);
        rbDownSendTo.setOnCheckedChangeListener(onListener1);
        rbListenSendTo.setOnCheckedChangeListener(onListener1);
        rbLikeSendTo.setOnCheckedChangeListener(onListener1);
        rbCmtLike.setOnCheckedChangeListener(onListener1);
        rbAddFans.setOnCheckedChangeListener(onListener1);
        rbMoney.setOnCheckedChangeListener(onListener1);

        kbiCoverSendTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 作品封面
                MAPP.startUserHomePage(mBAppUser, true);
            }
        });
        findViewById(R.id.kbtCopySendTo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 复制信息
                if (mBAppWorks != null) {
                    copyInfo();
                }
            }
        });
        findViewById(R.id.kbtStepSendTo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 间隔时间
                etStepSendTo();
            }
        });
        etStepSendTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 间隔时间
                etStepSendTo();
            }
        });
        findViewById(R.id.kbtQtySendTo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 发送数量
                etQtySendTo();
            }
        });
        etQtySendTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 发送数量
                etQtySendTo();
            }
        });


        kbtCommentSendTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 评论内容
                etCommentSendTo();
            }
        });
        etCommentSendTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 评论内容
                etCommentSendTo();
            }
        });

        kbtSendSendTo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {                   // 发送
                sendSelect();
            }
        });
        setAccount(allAccount);
    }

    @Override
    public void onInitData(){                                                                       // onInitView结束，开始加载数据

            String userIdS = MDBS.getFreeText(gWorksType, "OperationUser");
            String worksIdS = MDBS.getFreeText(gWorksType, "OperationWorks");
            if (userIdS != null) {
                mBAppUser = MDBS.getBAppUser(gWorksType, userIdS);
                if (worksIdS != null) {                          // 重新打开的
                    mBAppWorks = MDBS.getBAppWorks(gWorksType, userIdS, worksIdS);
                }
            }

        mMyWorks.setUiEventCallBack(new EventHandler() {
            @Override
            public void onComplete(int methodType, boolean isFail, String message) {
                if (methodType > 0) {
                }else {
                    if (mMyWorks.isEventDataRefresh()){
//                        showInfo();
                    }
                }
            }
        });
        getChecked();
        String text = MDBS.getFreeText(gWorksType, "OperationComment");
        makeComment(text);
        showInfo();
    }
    @Override
    public void onPageUpdate(BasePage basePage){
        Object object = getPageResult(basePage);
        if (object instanceof BAppUser) {
            mBAppUser = (BAppUser)object;
            MDBS.setBAppUser(mBAppUser);
            MDBS.setFreeText(gWorksType, "OperationUser", mBAppUser.UserIdS);
        }
        if (object instanceof BAppWorks) {
            mBAppWorks = (BAppWorks)object;
            MDBS.setBAppWorks(mBAppWorks);
            MDBS.setFreeText(gWorksType, "OperationWorks", mBAppWorks.WorksIdS);
        }
        onInitData();
    }
    private void showInfo() {

        if (mBAppWorks != null){
            MAPP.showWorksCover(mBAppWorks, mBAppWorks.CoverFile, kbiCoverSendTo);
            MAPP.showWorksName(mBAppWorks, kbtWorksSendTo);
            MSKIN.setText(MAPP.getWorks(mBAppWorks), tvStateSendTo);
            MSKIN.setText(mBAppWorks.Intro, etIntroSendTo);
        }else {
            MSKIN.showWorksCover((String) null, kbiCoverSendTo);
        }
        String step = MDBS.getFreeText( gWorksType, "OperationStep");
        if (MAPI.isEmpty(step)){
            step = "5";
            MDBS.setFreeText( gWorksType, "OperationStep", step);
            MDBS.setFreeText(gWorksType, "OperationQty", "10");
        }
        String qty = MDBS.getFreeText( gWorksType, "OperationQty");
        MSKIN.setText(step, etStepSendTo);
        MSKIN.setText(qty, etQtySendTo);
        String text = MDBS.getFreeText(gWorksType, "OperationComment");
        if (!MAPI.isEmpty(text)){
            MSKIN.setText(text, etCommentSendTo);
        }
        if (mComment != null){
            text = String.format("评论内容(%s行)：", mComment.length);
        }else {
            text = "评论内容：";
        }
        MSKIN.setText(text, kbtCommentSendTo);
    }

    private void copyInfo(){
        new TaskFree(null, new TaskFree.Listener() {
            @Override
            public void onMessage(boolean isThread, Object object) {
                if (isThread){
                    if (gWorksType < 0x20){
                        String fileName = mMyUtils.getWorksFileName(AFileType.FILE_LYRIC, mBAppWorks, MRAM.gsCachePath);
                        if (!MAPI.IS_FILE_DONE(fileName)) {
                            int type = mMyUtils.getWorksRoomType(AFileType.FILE_LYRIC);
                            ByteBuffer mRunState = ByteBuffer.allocateDirect(128);
                            String result = mMyUtils.threadDownloadFiles(type, mBAppWorks.UserID, mBAppWorks.FileID, 0,0, null, fileName, mRunState);  // (mRunState的值（0是继续1是退出）
                            MLOG(String.format("fileNameAA %s(%s)%s_%04x(%s)%s", result, type, mBAppWorks.UserID, mBAppWorks.FileID, mMyUtils.getServerNumber(), fileName));
                            if ("ok".equals(result)){
                                MAPI.SET_FILE_DONE(fileName);
                                doneKclFile(fileName);
                            }else {
                                AFileInfo aFileInfo = null;
                                for (int counter = 0; counter < 100; counter++)  {
                                    aFileInfo = MSKIN.getAFileInfo(mBAppWorks);
                                    MLOG(String.format("fileNameAB %s(%s)", counter, aFileInfo));
                                    if (aFileInfo != null){
                                        AFileBase aFileBase = new AFileBase();
                                        aFileBase.UserID = aFileInfo.MusicID0;
                                        aFileBase.FileID = aFileInfo.MusicID1;
                                        result = mMyUtils.threadDownloadFiles(type, aFileBase.UserID, aFileBase.FileID, 0,0, null, fileName, mRunState);  // (mRunState的值（0是继续1是退出）
                                        MLOG(String.format("fileNameAC %s(%s)%s_%04x(%s)%s", result, type, aFileBase.UserID, aFileBase.FileID, mMyUtils.getServerNumber(), fileName));
                                        if ("ok".equals(result)){
                                            MAPI.SET_FILE_DONE(fileName);
                                            doneKclFile(fileName);
                                        }
                                        break;
                                    }
                                    MSLEEP(100);
                                }

                            }
                        }else {
                            MLOG("fileNameAG " + fileName);
                            doneKclFile(fileName);
                        }
                    }
                    return;
                }
                SubUtils subUtils = new SubUtils();
                subUtils.copyToClipboard(gsLyric);
            }
        });
    }
    private void doneKclFile(String fileName){
        AKclFile aKclFile = MAPI.READ_KCL_FILE(fileName);
        if (aKclFile != null){
            MLOG(String.format("fileNameAD %s(%s)", aKclFile.Lines.length, fileName));
            StringBuffer stringBuffer = new StringBuffer();
//            String text = String.format("%s - %s", mBAppWorks.WorksName, mBAppWorks.ArtistName);
            stringBuffer.append(mBAppWorks.WorksName);
            stringBuffer.append("\n");
            stringBuffer.append(mBAppWorks.ArtistName);
            stringBuffer.append("\n");
            for (int index = 0; index < aKclFile.Lines.length; index++){
                stringBuffer.append(aKclFile.Lines[index].Text);
                stringBuffer.append("\n");
            }
            gsLyric = stringBuffer.toString();
        }
    }
    private void sendSelect(){
        if (mBAppUser == null){
            MTOAST(String.format("还没有选择用户", 2000));
            return;
        }
        if (mBAppWorks == null){
            if (gSelect < 3){
                MTOAST(String.format("还没有选择作品", 2000));
                return;
            }
        }
        kbtSendSendTo.setProgress(2);
        gSuccess = 0;
        MSKIN.setText(" ", tvSuccessSendTo);
        MTOAST(String.format("正在操作...", 2000));
        new TaskFree(null, new TaskFree.Listener() {
            @Override
            public void onMessage(boolean isThread, Object object) {
                if (isThread){
                    int step = MAPI.STRING_DEC32(MDBS.getFreeText(gWorksType, "OperationStep")) * 1000;
                    int qty = MAPI.STRING_DEC32(MDBS.getFreeText(gWorksType, "OperationQty"));
                    int error = 0;
                    gsMessage = "";
                    int[] commentID = null;
                    if (gSelect == 6){              // +评论点赞
                        BCommentInfo[] bCommentInfos = null;
                        if (gWorksType < 32) {
                            bCommentInfos = App32.getCommentList(mBAppWorks.UserIdS, mBAppWorks.WorksIdS, "0", 100);
                        }else if (gWorksType == 33) {
                            bCommentInfos = App33.getCommentList(0, mBAppWorks.UserIdS, mBAppWorks.WorksIdS, null);
                        }
                        if (bCommentInfos != null){
                            commentID = new int[bCommentInfos.length];
                            for (int index = 0; index < commentID.length; index++) {
                                commentID[index] = bCommentInfos[index].CommentID;
                            }
                        }
                    }
                    EditUtils editUtils = new EditUtils();
                    for (int index = 0; index < qty; index++) {
                        int appCount = gAppCount++ % gAppQty;
                        if (editUtils.waitWriteLock("上传_图片")) {
//                            BDbsAccount bDbsAccount = MAPP.mTaskEach[MAPP.getWorksTypeIndex(gWorksType)][index+1].mBDbsAccount; //  注意第0个不是正常的任务
//                            MLOG(String.format("setCommentA A %s %s %s %s", gWorksType, MAPP.getWorksTypeIndex(gWorksType), index, bDbsAccount));
                            App32.setUser(mBDbsAccount.get(index));
                            switch (gSelect){
                                case 0: gsResult = addComment(appCount); break;
                                case 1: gsResult = sendGifts(appCount); break;
                                case 2: gsResult = addDownload(appCount); break;
                                case 3: gsResult = addDownload(appCount); break;
                                case 4: gsResult = addListen(appCount); break;
                                case 5: gsResult = addLikeWorks(appCount); break;
                                case 6: gsResult = addLikeComment(appCount, commentID); break;
                                case 7: gsResult = addCareUser(appCount); break;
                            }
//                            MLOG(String.format("setCommentA A %s %s_%04x(%s)%s", bDbsAccount.AccountName, mBAppWorks.UserID, mBAppWorks.FileID, mBAppWorks.WorksName, gsResult));
                            editUtils.clearWriteLock();
                        }

                        MLOG(String.format("setCommentA B %s_%04x(%s)%s(%s)", mBAppWorks.UserID, mBAppWorks.FileID, mBAppWorks.WorksName, gSuccess, gsResult));
                        if (!"ok".equals(gsResult)){
                            if (++error > gAppQty) {
                                break;
                            }
                        }else {
                            ++gSuccess;
                        }
                        setMessage();
                        MSLEEP(step);
                    }
                    setMessage();
                    return;
                }
                kbtSendSendTo.setProgress(2 + 100);
                if ("ok".equals(gsResult)){
                    MTOAST(String.format("共成功%s次", gSuccess), 2000);
                }else {
                    MTOAST(String.format("出错，只成功%s次", gSuccess), 2000);
                }
            }
        });
    }
    private String addCareUser(int appCount){
        if (gWorksType < 32) {
            return App32.addCareUser(mBAppWorks.UserIdS);
        }
        if (gWorksType == 33) {
            return App33.addCareUser(mBAppWorks.UserIdS);
        }
        return null;
    }
    private String addComment(int appCount){
        gsMessage = mComment[gCounter++ % mComment.length];
        if (gWorksType < 32) {
            String result = App32.setComment(mBAppWorks.UserIdS, mBAppWorks.WorksIdS, null, gsMessage);
            MLOG(String.format("setCommentA setCommentA addComment %s %s %s", mBAppWorks.UserIdS, mBAppWorks.WorksIdS, gsMessage));
            return result;
        }
        if (gWorksType == 33) {
            return App33.setComment(mBAppWorks.UserIdS, mBAppWorks.WorksIdS, null, gsMessage);
        }
        return null;
    }
    private String addLikeWorks(int appCount){      // 增加点赞数量
        if (gWorksType < 32) {
            return App32.addLikeWorks(mBAppWorks.UserIdS, mBAppWorks.WorksIdS, 1);
        }
        if (gWorksType == 33) {
            return App33.addLikeWorks(mBAppWorks.WorksIdS);
        }
        return null;
    }
    private String addLikeComment(int appCount, int[] commentID) {      // 作品评论增加点赞数量
        if (gWorksType < 32) {
            String result = App32.addLikeComment(mBAppWorks.UserIdS, mBAppWorks.WorksIdS, commentID);
            MLOG(String.format("setCommentA addLikeComment %s_%04x(%s)%s(%s)", mBAppWorks.UserID, mBAppWorks.FileID, mBAppWorks.WorksName, result, commentID.length));
            return result;
        }
        return null;
    }

    private String sendGifts(int appCount){
        if (gWorksType < 32) {
            if (MAPP.mKcAGiftBean != null) {
                int giftID = MAPP.mKcAGiftBean[1].FileID;
                int amount = 1;
                return App32.sendGifts(mBAppWorks.UserIdS, mBAppWorks.WorksIdS, giftID, amount, null);
            }else {
                MLOGE("App32 sendGifts示准备好");
            }
        }else if (gWorksType == 33) {
            String result = App33.sendGifts(mBAppWorks.WorksIdS);
            if ("0".equals(result)){
                return "ok";
            }else if (MAPI.STRING_DEC32(result) > 0){
                return "ok";
            }
            return result;
        }
        return null;
    }
    private String addDownload(int appCount){           // 增加下载数量
        if (gWorksType < 32) {
            return App32.addDownloadWorks(mBAppWorks.UserIdS, mBAppWorks.WorksIdS, 1);
        }
        return null;
    }
    private String addListen(int appCount){             // 增加聆听数量
        if (gWorksType < 32) {
            return App32.addListenWorks(mBAppWorks.UserIdS, mBAppWorks.WorksIdS, 1);
        }
        return null;
    }
    private void setSelect(int select){
        gSelect = select;
        clCommentSendTo.setVisibility(View.GONE);
        clGiftSendTo.setVisibility(View.GONE);
        clLikeSendTo.setVisibility(View.GONE);
        switch (select){
            case 0: clCommentSendTo.setVisibility(View.VISIBLE); break;
            case 1: clGiftSendTo.setVisibility(View.VISIBLE); break;
            case 2:
                clLikeSendTo.setVisibility(View.VISIBLE);
                MSKIN.setText("增加转发数量", kbtLikeSendTo);
                break;
            case 3:
                clLikeSendTo.setVisibility(View.VISIBLE);
                MSKIN.setText("增加下载数量", kbtLikeSendTo);
                break;
            case 4:
                clLikeSendTo.setVisibility(View.VISIBLE);
                MSKIN.setText("增加聆听数量", kbtLikeSendTo);
                break;
            case 5:
                clLikeSendTo.setVisibility(View.VISIBLE);
                MSKIN.setText("增加作品点赞数量", kbtLikeSendTo);
                break;
            case 6:
                clLikeSendTo.setVisibility(View.VISIBLE);
                MSKIN.setText("增加评论点赞数量", kbtLikeSendTo);
                break;
            case 7:
                //                rbAddFans
                clLikeSendTo.setVisibility(View.VISIBLE);
                MSKIN.setText("增加评论点赞数量", kbtLikeSendTo);
                break;
            case 8:
//                rbMoney ;
                clLikeSendTo.setVisibility(View.VISIBLE);
                MSKIN.setText("增加评论点赞数量", kbtLikeSendTo);
                break;
        }
        MSKIN.setText(" ", tvSuccessSendTo);
    }
    private void setAccount(boolean allAccount){
        if (!allAccount) {
//            gAppQty = 1;
            mBDbsAccount = new ArrayList<>();
            mBDbsAccount.add(MDBS.getBDbsAccount(gWorksType, MAPP.mBDbsAccount.AccountName));
        }else {
            mBDbsAccount = MDBS.getBDbsAccount(gWorksType);
//            gAppQty = MAPP.mTaskEach[MAPP.getWorksTypeIndex(gWorksType)].length;
        }
        gAppQty = mBDbsAccount.size();
    }


    private void etStepSendTo(){
        String text = etStepSendTo.getText().toString();
        MSKIN.startSkinInputEmoji(false, 100, "间隔时间(秒)", text, new KcTwoListener() {
            @Override
            public void onMessage(Object object1, Object object2) {
                String text = (String) object2;
                etStepSendTo.setText(text);
                MDBS.setFreeText(gWorksType, "OperationStep", text);
            }
        });
    }
    private void etQtySendTo(){
        String text = etQtySendTo.getText().toString();
        MSKIN.startSkinInputEmoji(false, 100, "发送数量", text, new KcTwoListener() {
            @Override
            public void onMessage(Object object1, Object object2) {
                String text = (String) object2;
                etQtySendTo.setText(text);
                MDBS.setFreeText(gWorksType, "OperationQty", text);
            }
        });
    }
    private void etCommentSendTo(){
        String text = etCommentSendTo.getText().toString();
//        MLOG("text1 "+text);
        MSKIN.startSkinInputEmoji(false, 10000, "评论内容", text, new KcTwoListener() {
            @Override
            public void onMessage(Object object1, Object object2) {
                String text = (String) object2;
                makeComment(text);
                if (mComment != null) {
                    StringBuffer stringBuffer = new StringBuffer();
                    for (int index = 0; index < mComment.length; index++){
                        stringBuffer.append(mComment[index]);
                        stringBuffer.append("\n");
                    }
                    MDBS.setFreeText(gWorksType, "OperationComment", stringBuffer.toString());
                }
                showInfo();
            }
        });
    }
    private void setMessage(){
        MRAM.mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                int number = gAppCount % gAppQty;
                if ("ok".equals(gsResult)) {
                    if ("ok".equals(gsMessage)) {
                        MSKIN.setText(String.format("#%s成功：%s[%s]", number, gSuccess, gsMessage), tvSuccessSendTo);
                    }else {
                        MSKIN.setText(String.format("#%s成功：%s", number, gSuccess), tvSuccessSendTo);
                    }
                }else {
                    MSKIN.setText(String.format("#%s出错：%s\n已成功：%s", number, gsResult, gSuccess), tvSuccessSendTo);
                }
            }
        });

    }
    private void makeComment(String text){
        if (!MAPI.isEmpty(text)){
            text = text.replace("    ", "\n");
            text = text.replace("\n\n", "\n");
            text = text.replace("\n\n", "\n");
            text = text.replace("\n ", "\n");
            text = text.replace(" \n ", "\n");
            text = text.replace("  ", " ");
            text = text.replace("  ", " ");
            text = text.replace("  ", " ");
            mComment = text.split("\n");
        }
    }

    private void getChecked(){
        int[] tabViewId1 = {R.id.rbLikeSendTo, R.id.rbComment, R.id.rbGiftSendTo, R.id.rbShareSendTo, R.id.rbDownSendTo, R.id.rbListenSendTo};
        int select = MDBS.getFreeInt(gWorksType, "OperationSelect");
        for (int index = 0; index < tabViewId1.length; index++){
            RadioButton radioButton = ((RadioButton)findViewById(tabViewId1[index]));
            if (select == index){
                radioButton.setChecked(true);
            }else {
                radioButton.setChecked(false);
            }
        }
        setSelect(select);
    }


}

